//错误处理
const errorType = require('../constant/errTypes');
const errorHandler = (error,ctx) => {
  switch (error.message) {
    case errorType.NAME_OR_PASSWORD_IS_ALREADY_EXIST:
      ctx.status = 404;
      ctx.body = {
        code: 404,
        message: '用户名或密码已存在'
      };
      break;
    case errorType.NAME_OR_PASSWORD_IS_REQUIRED:
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '用户名或密码不能为空'
      };
      break;
    case errorType.NAME_OR_PASSWORD_IS_EXIST:
      ctx.status = 409;
      ctx.body = {
        code: 409,
        message: '用户名已存在'
      };
      break;
    case errorType.NAME_OR_PASSWORD_IS_NOT_MATCH:
      ctx.status = 401;
      ctx.body = {
        code: 401,
        message: '用户名或密码错误'
      };
      break;
    case errorType.TOKEN_IS_INVALID:
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: 'token无效'
      };
      break;
      case errorType.UNPERMISSION:
      ctx.status = 401;
      ctx.body = {
        code: 401,
        message: '无权限'
      };
      break;
      case errorType.LABEL_NOT_EXIST:
      ctx.status = 404;
      ctx.body = {
        code: 404,
        message: '标签不存在'
      };
      break;
    // case errorType.SERVER_ERROR:
    //   ctx.status = 500;
    //   ctx.body = {
    //     code: 500,
    //     message: 'Server Error'
    //   };
    //   break;
    default:
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: 'Server Error'
      };
      break;
  }
  console.log(error);
  return error;
};

module.exports = errorHandler;